package com.bdqn.shiro.controller;



import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.shiro.pojo.CstService;
import com.bdqn.shiro.pojo.User;
import com.bdqn.shiro.service.BasDictService;
import com.bdqn.shiro.service.CstCustomerService;
import com.bdqn.shiro.service.CstServiceService;
import com.bdqn.shiro.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ServiceController {

    @Resource
    private UserService userService;
    @Resource
    private CstServiceService cstServiceService;
    @Resource
    private BasDictService basDictService;
    @Resource
    private CstCustomerService cstCustomerService;

    @RequestMapping("service/deal")
    public String List(Model model, CstService cstService, @RequestParam(defaultValue = "1") Long pageIndex){
        Page page=new Page();
        page.setCurrent(pageIndex);
        page.setRecords(cstServiceService.getList(page,cstService));
        model.addAttribute("page",page);
        model.addAttribute("dicts",basDictService.list());
        return "service/deal";
    }

    @GetMapping("service/edit")
    public String edit(Model model,CstService cstService){
        model.addAttribute("serviced",cstServiceService.getById(cstService.getSvrId()));
        return "service/dealEdit";
    }


    @PostMapping("service/editDeal")
    public String editDeal(CstService cstService){
        if (cstServiceService.updateById(cstService)){
            return "redirect:deal";
        }
        return "service/dealEdit";
    }

    @GetMapping("service/add")
    public String toAdd(Model model){
        model.addAttribute("customers",cstCustomerService.list());
        model.addAttribute("dicts",basDictService.list());
        return "service/add";
    }
    @PostMapping("service/save")
    public String doSave(CstService cstService) {
        if (cstServiceService.save(cstService)) {
            return "redirect:deal";
        }
        return "redirect:edit";
    }

    @RequestMapping("service/feedback")
    public String Feedback(Model model,CstService cstService,@RequestParam(defaultValue = "1")Long pageIndex){
        Page page=new Page();
        page.setCurrent(pageIndex);
        page.setRecords(cstServiceService.getList(page,cstService));
        model.addAttribute("page",page);
        model.addAttribute("dicts",basDictService.list());
        return "service/feedback";
    }

    @RequestMapping("service/arch")
    public String toArch(Model model,CstService cstService,@RequestParam(defaultValue = "1")Long pageIndex){
        Page page=new Page();
        page.setCurrent(pageIndex);
        page.setRecords(cstServiceService.getList(page,cstService));
        model.addAttribute("page",page);
        model.addAttribute("dicts",basDictService.list());
        return "service/arch";
    }

    @GetMapping("service/toFeedBack")
    public String toFeedBack(Model model,CstService cstService){
        model.addAttribute("serviced",cstServiceService.getById(cstService.getSvrId()));
        return "service/feedbackEdit";
    }

    @PostMapping("service/editFB")
    public String editFB(CstService cstService){
        if (cstServiceService.updateById(cstService)){
            return "redirect:feedback";
        }
        return "redirect:toFeedBack";
    }

    @GetMapping("service/findArch")
    public String findArch(Model model,CstService cstService){
        model.addAttribute("serviced",cstServiceService.getById(cstService));
        return "service/archInfo";
    }

    @RequestMapping("service/dispatch")
    public String dispatch(Model model,CstService cstService,@RequestParam(defaultValue = "1")Long pageIndex){
        Page page=new Page();
        page.setCurrent(pageIndex);
        page.setRecords(cstServiceService.getList(page,cstService));
        model.addAttribute("page",page);
        model.addAttribute("dicts",basDictService.list());
        model.addAttribute("users",userService.list());
        return "service/dispatch";
    }


    @GetMapping("service/allot/{svrId}/{svrDueId}")
    @ResponseBody
    public Map allot(Model model, @PathVariable("svrId") Long svrId,@PathVariable("svrDueId")Long svrDueId){
        CstService cstService=new CstService();
        cstService.setSvrId(svrId);
        cstService.setSvrDueId(svrDueId);
        Map map=new HashMap();
        if (cstServiceService.updateById(cstService)){
            map.put("editResult","true");
        }
        return map;
    }

    @ResponseBody
    @GetMapping("service/del/{svrId}")
    public Map del(@PathVariable("svrId") Long svrId){
        Map map=new HashMap();
        if (cstServiceService.removeById(svrId)){
            map.put("delResult","true");
        }
        return map;
    }

    @RequestMapping("plan/list")
    public String PlanList(Model model, CstService cstService, @RequestParam(defaultValue = "1") Long pageIndex){
        Page page=new Page();
        page.setCurrent(pageIndex);
        page.setRecords(cstServiceService.getList(page,cstService));
        model.addAttribute("page",page);
        model.addAttribute("dicts",basDictService.list());
        return "plan/list";
    }
}
